
var app = getApp();
var shareTickets =""
Page({
  data: {
    index: 1,
	source: app.globalData.source,
    shareList: [{
        name: "哈佛家训",
        dateTime: "23:00:00",
        image: "my_share.png",
        status: 1
      },
      {
        name: "鬼吹灯",
        dateTime: "已结束",
        image: "my_share.png",
        status: 2
      },
      {
        name: "哈利波特",
        dateTime: "22:00:00",
        image: "my_share.png",
        status: 2
      }
    ]
  },
  bindGetUserInfo: function (e) {
    getApp().authInfo(e);
  },
  onLoad: function(e) {
    var that = this;
	  if (e.status != 0){ //从我的页面进入我的分享，不是从群里边进入
		that.getGroupID(); //调用获取群编号的方法
	  }
    var shareList = that.data.shareList;
    that.setData({
      shareList: shareList
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
	  wx.startPullDownRefresh()
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
	  var that = this;
	  wx.showShareMenu({
		  withShareTicket: true
	  })
	  return {
		  title: app.globalData.userInfo.nickName+'邀您一起听',
		  path: '/pages/my/my_share/my_share',
		  imageUrl:'../../../images/img.png',
		  success(res) {
			  shareTickets = res.shareTickets;
			  console.log("shareTickets="+shareTickets)
			  if (shareTickets == undefined) { //执行个人分享保存
			  console.log("进入个人。。。。。。。。。。。。。。")
				  //分装提交数据 shareSource 栏目id  shareStatus分享状态
				  var datas = { userId: app.globalData.userId, programId: "56", shareSource: "0", shareStatus: "0", survivalTime:"627266776"} 
				  that.singleGroupId(datas); //调取保存用户分享的栏目

			  }else{ //执行群分享保存
				  var datas = { userId: app.globalData.userId, programId: "56", shareSource: "1", shareStatus: "1", survivalTime: "627266776" } 
				  that.getGroupID(datas); //调用获取群编号的方法
			  }
			  
		  },
		  fail(e){
			
		  },
		  complete(){

		  }
	  }

  },

	getGroupID:function(datas){ //获取分享群id
		wx.getShareInfo({
			shareTicket: shareTickets[0],
			success: function (res) {
				console.log("=================*===============")
					console.log(shareTickets[0])
				console.log("==================*==============")
				var encryptedData = res.encryptedData;
				console.log(res)
				var iv = res.iv;
				wx.login({
					success: function (res) {
						var code = res.code;
						console.log(code)
						var userId = datas.userId;
						var programId = datas.programId;
						var shareSource = datas.shareSource;
						var shareStatus = datas.shareStatus;
						var survivalTime = datas.survivalTime;
						var data = { 'wxEncryptedData': encryptedData, "wxIv": iv, userId: userId, programId: programId, shareSource: shareSource, shareStatus: shareStatus, survivalTime: survivalTime,wxCode:code};
						console.log(data)
						app.request(app.globalData.https + "/programResource/myProgramShare", //处理获取分享群信息
							"post", data, "", function (res) {
								//if (res.code == 0) {
									console.log("调用接口"+res)
								//}
							}, function (res) {
								console.log(res)
							}
						)
					}
				})
			}
		})
	},
	/**
	 * 个人用户分享
	 */
	singleGroupId:function(data){
		app.request(app.globalData.https + "programResource/myProgramShare", //处理获取分享群信息
			"post", data, "", function (res) {
				if (res.code == 0) {
					console.log("调用接口" + res)
				}
			}, function (res) {
				console.log(res)
			}
		)
	},

  shareDetailBindTap: function(e) {
    var id = e.currentTarget.dataset.id;
    console.log(id)
    wx.navigateTo({
      url: "../../my/my_share/sharing_details/sharing_details",
      success: function() {   
      },
      fail: function() {
      }
    })
  }
})